package com.servlet;

import com.dao.Employee_infoDao;
import com.dao.UserDao;
import com.dao.impl.Employee_infoDaoImpl;
import com.dao.impl.UserDaoImpl;
import com.entry.Employee_info;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@WebServlet(urlPatterns = "/man_add.do")
public class Man_addServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        System.out.println(123);
        HttpSession session = req.getSession();
        JsonParser jParser = new JsonParser();
        JsonObject jt = (JsonObject) jParser.parse(req.getParameter("em_info"));//构造JsonObject对象
        Employee_info e = new Employee_info();

        String username = jt.get("username").getAsString();
        String ename = jt.get("ename").getAsString();
        String idcode = jt.get("idcode").getAsString();
        String ssex = jt.get("ssex").getAsString();
        String nation = jt.get("nation").getAsString();
        String political = jt.get("political").getAsString();
        String birthday = jt.get("birthday").getAsString();
        String homeaddr = jt.get("homeaddr").getAsString();
        String mobile = jt.get("mobile").getAsString();
        String zipcode = jt.get("zipcode").getAsString();
        String conaddr = jt.get("conaddr").getAsString();
        String receiver = jt.get("receiver").getAsString();
        e.setUsername(username);
        e.setEname(ename);
        e.setIdcode(idcode);
        e.setSsex(ssex);
        e.setNation(nation);
        e.setPolitical(political);
        e.setBirthday(birthday);
        e.setHomeaddr(homeaddr);
        e.setMobile(mobile);
        e.setZipcode(zipcode);
        e.setConaddr(conaddr);
        e.setReceiver(receiver);

        String mess = validateForm(e);
        System.out.println(mess);
        if ("".equals(mess)){
            session.setAttribute("infoMess", mess);
            resp.sendRedirect("./man_add.jsp");
        }else{
            //调用员工信息添加方法
            Employee_infoDao emp = new Employee_infoDaoImpl();
            int i = emp.add(e);
            System.out.println("员工信息添加"+i);
            if (i != 0) {
                UserDao uImpl = new UserDaoImpl();
                uImpl.add(e.getUsername());
                session.setAttribute("infoMess", "添加成功！");
                resp.sendRedirect("./man_manage/man_uphold.jsp");
            } else {
                session.setAttribute("infoMess", "添加失败，请检查！");
                resp.sendRedirect("./man_manage/man_add.jsp");
            }
        }
    }

    private String validateForm(Employee_info emp) {
        if ("".equals(emp.getUsername())||emp.getUsername()==null){
            return "*用户名不能为空！";
        }
        if (emp.getIdcode() == null || emp.getIdcode().matches("\\w{15,18}")) {
            return "*身份证输入有误！";
        }
        return "";
    }
}
